package com.hp.config.rpc;


import com.hp.client.LarkRobotClient;
import com.hp.config.properties.LarkRobotProperties;
import feign.RequestInterceptor;
import jakarta.annotation.Resource;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author hp
 */
@Configuration(proxyBeanMethods = false)
@EnableFeignClients(clients = {LarkRobotClient.class})
@EnableConfigurationProperties(LarkRobotProperties.class)
public class RpcConfiguration {

    @Resource
    private LarkRobotProperties larkRobotProperties;

    /**
     * 此拦截器会全局生效，一定要加控制好范围，否则会影响到其他模块的调用
     */
    @Bean
    public RequestInterceptor urlInterceptor() {

        // 动态修改 lark机器人的webapi
        return template -> {
            if ("lark-robot-client".equals(template.feignTarget().name())) {
                String newUrl = larkRobotProperties.getWebapi();
                template.target(newUrl);
            }
        };
    }

}
